<?php
namespace App\Http\Logic;

use App\Models\SwtCountry;
use App\Services\EasyWechatService;
use App\Services\EmailService;
use App\Services\SmsService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use TheSeer\Tokenizer\Exception;

class UtilLogic extends BaseLogic
{
    public static function queryCountries() : array
    {
        $countriesByLetter = SwtCountry::select(
            'english_name',
            'first_name',
            'country_name',
            'id',
            'icon',
            'telephone_code'
            )
            ->orderBy('english_name')
            ->get()
            ->groupBy('first_name')
            ->toArray();

        $conuntries = [];

        $lang = App::getLocale();

        foreach($countriesByLetter as $letter => $countries) {
            $conuntries[$letter] = array_map(function ($value) use($lang) {
                return [
                    'name'           => $lang == "en" ? $value['english_name'] : $value['country_name'],
                    'id'             => $value['id'],
                    'icon'           => config('app.url') . '/storage' . $value['icon'],
                    'telephone_code' => $value['telephone_code'],
                ];
            }, $countries);
        }

        return $conuntries;
    }

    public static function getCountryInfo(int $id) : array
    {
        $info = SwtCountry::find($id)->toArray();

        $info['icon'] = config('app.url') . '/storage' . $info['icon'];

        return $info;
    }

    public static function  sendSmsCode(string $phone, string $type) : bool
    {
        $code = mt_rand(100000, 999999);

        $res = SmsService::sendSms($phone, $code);

        if(false === $res) {
            self::$error = SmsService::$errorMessage;
            return false;
        }

        switch($type) {
            case "login" :
                $name = 'sms_code_' . $phone;
            break;
            case "set_password" :
                $name = 'sms_code_set_password_' . $phone;
                break;
            case "forget_password" :
                $name = 'sms_code_update_' . $phone;
            break;
            case "set_pay_password" :
                $name = 'sms_code_pay_password_' . $phone;
            break;
            case "update_phone" :
                $name = 'sms_code_change_phone_' . $phone;
            break;
            case "redeem_pledge" :
                $name = 'sms_code_redeem_pledge_' . $phone;
            break;
        }

        Cache::put($name, $code, 300);

        return true;
    }

    public static function checkSmsCode(string $phone, string $code, string $type) : bool
    {
        switch($type) {
            case "login" :
                $name = 'sms_code_' . $phone;
                break;
            case "set_password" :
                $name = 'sms_code_set_password_' . $phone;
                break;
            case "forget_password" :
                $name = 'sms_code_update_' . $phone;
                break;
            case "set_pay_password" :
                $name = 'sms_code_pay_password_' . $phone;
                break;
            case "update_phone" :
                $name = 'sms_code_change_phone_' . $phone;
                break;
            case "redeem_pledge" :
                $name = 'sms_code_redeem_pledge_' . $phone;
                break;
        }

        $cache_code = Cache::get($name);

        if($cache_code !== $code) {
            self::$error = __("auth.sms_code_error");
            return false;
        }

        Cache::forget($name);

        return true;
    }


    public static function sendEmailCode(string $email, string $type) : bool
    {
        $code = mt_rand(100000, 999999);

        $res = EmailService::sendEmail($email, $code);

        if(false === $res) {
            self::$error = EmailService::$errorMessage;
            return false;
        }

        switch($type) {
            case "register" :
                $name = 'email_code_' . $email;
            break;
            case "forget" :
                $name = 'email_code_forget_password_' . $email;
                break;
            case "pay_password" :
                $name = "email_code_pay_password_" . $email;
                break;
        }

        Cache::put($name, $code, 300);

        return true;
    }

    public static function upload(Object $file) : array
    {
        $path = $file->store('uploads/idcard', 'public');

        return ['url' => config('app.url').'/storage/'.$path];
    }

    public static function analysisIdCard(string $type, string $url) : array | bool
    {
        try {
            $info = EasyWechatService::analysisIdCard($type, $url);
            return $info;
        } catch (Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }
}
